*Figure 6
clear
set more off
insheet using counterfactual.csv

gen competition = _n
rename v1 mergers0
rename v2 mergers1
rename v3 subs0
rename v4 subs1
rename v5 maxs0
rename v6 maxs1

gen log_subs_diff = log(subs0) - log(subs1)
sum log_subs_diff
cdfplot log_subs_diff, text(0.95 -0.45 `"mean = `=string(`=r(mean)',"%6.2f")'"', color(red) placement(se)) xline(`=r(mean)') xline(0, lpattern(dash)) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ytitle(, size(large)) xtitle("Difference in submissions (log points)", size(large)) ylabel(,nogrid labsize(large)) xlabel(,nogrid labsize(large))   scheme(sj) legend(off)
graph export Output/Figure6A.pdf, replace

gen maxs_diff = maxs0-maxs1
sum maxs_diff
cdfplot maxs_diff, text(1 0.025 `"mean = `=string(`=r(mean)',"%6.2f")'"', color(red) placement(se)) xline(`=r(mean)') xline(0, lpattern(dash)) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ytitle(, size(large)) xtitle("Difference in max score (st. deviations)", size(large)) ylabel(,nogrid labsize(large)) xlabel(,nogrid labsize(large))   scheme(sj) legend(off)
graph export Output/Figure6B.pdf, replace



*Figure 4
clear
set more off
insheet using fit.csv

rename v1 mergers_d
rename v2 mergers_m
rename v3 subs_d
rename v4 subs_m
rename v5 maxs_d
rename v6 maxs_m

bys mergers_d: gen ww_m=_N
bys subs_d: gen ww_s=_N
bys maxs_d: gen ww_ms=_N
two (scatter mergers_d mergers_m [w=ww_m]) || (function y = x, ra(mergers_d) clpat(dash)) ,   graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ytitle("Number of multiplayer teams (data)", size(large)) xtitle("Number of multiplayer teams (model)", size(large)) ylabel(,nogrid labsize(large)) xlabel(,nogrid labsize(large))   scheme(sj) legend(off)
graph export Output/Figure4A.pdf, replace
two (scatter subs_d subs_m [w=ww_s]) || (function y = x, ra(subs_d) clpat(dash)) ,   graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ytitle("Number of submissions (data)", size(large)) xtitle("Number of submissions (model)", size(large)) ylabel(,nogrid labsize(large)) xlabel(,nogrid labsize(large))   scheme(sj) legend(off)
graph export Output/Figure4B.pdf, replace
two (scatter maxs_d maxs_m [w=ww_ms]) || (function y = x, ra(maxs_d) clpat(dash)) if maxs_d<60 ,   graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ytitle("Max score (data)", size(large)) xtitle("Max score (model)", size(large)) ylabel(,nogrid labsize(large)) xlabel(,nogrid labsize(large))   scheme(sj) legend(off)
graph export Output/Figure4C.pdf, replace





***Figure 5
clear
insheet using figure_out_N.csv
keep if _n<=19

gen teams = _n-1
keep if _n>1
two (line v1 teams) (line v3 teams) ,    graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ytitle("Probability of forming a team", size(large)) xtitle("Number of teams", size(large)) ylabel(,nogrid labsize(large)) xlabel(,nogrid labsize(large))   scheme(sj) legend(label(1 "Low score")  label(2 "High score")) 
graph export Output/Figure5A.pdf, replace
two (line v4 teams) (line v6 teams) ,    graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ytitle("Probability of submission", size(large)) xtitle("Number of teams", size(large)) ylabel(,nogrid labsize(large)) xlabel(,nogrid labsize(large))   scheme(sj) legend(label(1 "Low score")  label(2 "High score")) 
graph export Output/Figure5C.pdf, replace

clear
insheet using figure_out_T.csv
keep if _n<359
gen time = (_n)/360
two (line v1 time)  (line v3 time) ,    graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ytitle("Probability of forming a team", size(large)) xtitle("Elapsed time (fraction)", size(large)) ylabel(,nogrid labsize(large)) xlabel(,nogrid labsize(large))   scheme(sj) legend(label(1 "Low score") label(2 "High score")) 
graph export Output/Figure5B.pdf, replace
two (line v4 time)  (line v6 time) ,    graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ytitle("Probability of submission", size(large)) xtitle("Elapsed time (fraction)", size(large)) ylabel(,nogrid labsize(large)) xlabel(,nogrid labsize(large))   scheme(sj) legend(label(1 "Low score") label(2 "High score")) 
graph export Output/Figure5D.pdf, replace


*Figure A4
clear
insheet using figure_out_N2.csv
keep if _n<=19
gen teams = _n-1
keep if _n>1
two (line v1 teams) (line v2 teams)  (line v3 teams)  (line v4 teams) ,    graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ytitle("Probability of submission", size(large)) xtitle("Number of teams", size(large)) ylabel(,nogrid labsize(large)) xlabel(,nogrid labsize(large))   scheme(sj) legend(label(1 "Team/Low score") label(2 "Team/High score") label(3 "Solo player/Low score") label(4 "Solo player/High score")) 
graph export Output/FigureA4.pdf, replace



**********Figure 8
***subs
clear
set more off
insheet using counter_out_cost.csv

matrix median = J(1,5,.)
matrix colnames median = "10" "20" "30"  "40" "50" 

matrix CI = J(2,5,.)

matrix colnames CI = "10" "20" "30"  "40" "50"  
matrix rownames CI = ll95 ul95

local i 0
foreach k in 11 10 9 8 7 {
local ++ i
gen diff`k' = log(v`k')-log(v12)
reg diff`k', r
matrix median[1, `i'] = 100*_b[_cons]
matrix CI[1, `i'] = 100*(_b[_cons]-2*_se[_cons])
matrix CI[2, `i'] = 100*(_b[_cons]+2*_se[_cons])
}

matrix list median
matrix list CI

coefplot matrix(median), ci(CI) vertical ///
ytitle("Percentage change in # of submissions",size(large)) ///
xtitle("Decrease in average cost of team formation",size(large)) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
ylabel(,nogrid labsize(large)) xlabel(,nogrid labsize(large))   scheme(sj) 
graph export Output/Figure8B.pdf, replace

***maxs
clear
set more off
insheet using counter_out_cost.csv

matrix median = J(1,5,.)
matrix colnames median = "10" "20" "30"  "40" "50" 

matrix CI = J(2,5,.)

matrix colnames CI = "10" "20" "30"  "40" "50"  
matrix rownames CI = ll95 ul95

local i 0
foreach k in 17 16 15 14 13 {
local ++ i
gen diff`k' = (v`k')-(v18)
reg diff`k', r
matrix median[1, `i'] = _b[_cons]
matrix CI[1, `i'] = (_b[_cons]-2*_se[_cons])
matrix CI[2, `i'] = (_b[_cons]+2*_se[_cons])
}

matrix list median
matrix list CI

coefplot matrix(median), ci(CI) vertical ///
ytitle("Change in max score (in st. deviations)",size(large)) ///
xtitle("Decrease in average cost of team formation",size(large)) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
ylabel(,nogrid labsize(large)) xlabel(,nogrid labsize(large))   scheme(sj) 
graph export Output/Figure8C.pdf, replace

***mergers
clear
set more off
insheet using counter_out_cost.csv

matrix median = J(1,5,.)
matrix colnames median = "10" "20" "30"  "40" "50" 

matrix CI = J(2,5,.)

matrix colnames CI = "10" "20" "30"  "40" "50"  
matrix rownames CI = ll95 ul95

local i 0
foreach k in 5 4 3 2 1 {
local ++ i
gen diff`k' = log(v`k')-log(v6)
reg diff`k', r
matrix median[1, `i'] = 100*_b[_cons]
matrix CI[1, `i'] = 100*(_b[_cons]-2*_se[_cons])
matrix CI[2, `i'] = 100*(_b[_cons]+2*_se[_cons])
}

matrix list median
matrix list CI

coefplot matrix(median), ci(CI) vertical ///
ytitle("Percentage change in # of teams",size(large)) ///
xtitle("Decrease in average cost of team formation",size(large)) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
ylabel(,nogrid labsize(large)) xlabel(,nogrid labsize(large))   scheme(sj) 
graph export Output/Figure8A.pdf, replace

**********Figure 7: Counterfactual: change in N
***subs
clear
set more off
insheet using counter_out_N.csv

matrix median = J(1,4,.)
matrix colnames median = "-10" "-5" "5"  "10" 

matrix CI = J(2,4,.)

matrix colnames CI = "-10" "-5" "5"  "10"  
matrix rownames CI = ll95 ul95

local i 0
foreach k in 6 7 9 10 {
local ++ i
gen diff`k' = log(v`k')-log(v8)
reg diff`k', r
matrix median[1, `i'] = 100*_b[_cons]
matrix CI[1, `i'] = 100*(_b[_cons]-2*_se[_cons])
matrix CI[2, `i'] = 100*(_b[_cons]+2*_se[_cons])
}

matrix list median
matrix list CI

coefplot matrix(median), ci(CI) vertical  ///
ytitle("Percentage change in # of submissions",size(large)) ///
xtitle("Percentage change in # of competitors",size(large)) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
ylabel(,nogrid labsize(large)) xlabel(,nogrid labsize(large))   scheme(sj) 
graph export Output/Figure7C.pdf, replace

***maxs
clear
set more off
insheet using counter_out_N.csv

matrix median = J(1,4,.)
matrix colnames median = "-10" "-5" "5"  "10" 

matrix CI = J(2,4,.)

matrix colnames CI = "-10" "-5" "5"  "10"  
matrix rownames CI = ll95 ul95

local i 0
foreach k in 11 12 14 15{
local ++ i
gen diff`k' = (v`k')-(v13)
reg diff`k', r
matrix median[1, `i'] = 1*_b[_cons]
matrix CI[1, `i'] = 1*(_b[_cons]-2*_se[_cons])
matrix CI[2, `i'] = 1*(_b[_cons]+2*_se[_cons])
}

matrix list median
matrix list CI

coefplot matrix(median), ci(CI) vertical  ///
ytitle("Change in max score (in st. deviations)",size(large)) ///
xtitle("Percentage change in # of competitors",size(large)) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
ylabel(,nogrid labsize(large)) xlabel(,nogrid labsize(large))   scheme(sj) 
graph export Output/Figure7E.pdf, replace


***mergers
clear
set more off
insheet using counter_out_N.csv

matrix median = J(1,4,.)
matrix colnames median = "-10" "-5" "5"  "10" 

matrix CI = J(2,4,.)

matrix colnames CI = "-10" "-5" "5"  "10"  
matrix rownames CI = ll95 ul95
local i 0
foreach k in 1 2 4 5{
local ++ i
gen diff`k' = log(v`k')-log(v3)
reg diff`k', r
matrix median[1, `i'] = 100*_b[_cons]
matrix CI[1, `i'] = 100*(_b[_cons]-2*_se[_cons])
matrix CI[2, `i'] = 100*(_b[_cons]+2*_se[_cons])
}

matrix list median
matrix list CI

coefplot matrix(median), ci(CI) vertical  ///
ytitle("Percentage change in # of teams",size(large)) ///
xtitle("Percentage change in # of competitors",size(large)) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
ylabel(,nogrid labsize(large)) xlabel(,nogrid labsize(large))   scheme(sj) 
graph export Output/Figure7A.pdf, replace
*yline(0, lwidth(thin) lpatter(dash) lcolor(gray))

***mergers per player
clear
set more off
insheet using counter_out_N.csv

matrix median = J(1,4,.)
matrix colnames median = "-10" "-5" "5"  "10" 

matrix CI = J(2,4,.)

matrix colnames CI = "-10" "-5" "5"  "10"  
matrix rownames CI = ll95 ul95

gen v1b=v1/36
gen v2b=v2/38
gen v3b=v3/40
gen v4b=v4/42
gen v5b=v5/44

local i 0
foreach k in 1 2 4 5 {
local ++ i
gen diff`k' = log(v`k'b)-log(v3b)
reg diff`k', r
matrix median[1, `i'] = 100*_b[_cons]
matrix CI[1, `i'] = 100*(_b[_cons]-2*_se[_cons])
matrix CI[2, `i'] = 100*(_b[_cons]+2*_se[_cons])
}

matrix list median
matrix list CI

coefplot matrix(median), ci(CI) vertical  ///
ytitle("Percentage change in share of teams",size(large)) ///
xtitle("Percentage change in # of competitors",size(large)) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
ylabel(,nogrid labsize(large)) xlabel(,nogrid labsize(large))   scheme(sj) 
graph export Output/Figure7B.pdf, replace

***subs per T
clear
set more off
insheet using counter_out_N.csv

matrix median = J(1,4,.)
matrix colnames median = "-10" "-5" "5"  "10" 

matrix CI = J(2,4,.)

matrix colnames CI = "-10" "-5" "5"  "10"  
matrix rownames CI = ll95 ul95

gen v6b=v1/36
gen v7b=v2/38
gen v8b=v3/40
gen v9b=v4/42
gen v10b=v5/44

local i 0
foreach k in 6 7 9 10 {
local ++ i
gen diff`k' = log(v`k'b)-log(v8b)
reg diff`k', r
matrix median[1, `i'] = 100*_b[_cons]
matrix CI[1, `i'] = 100*(_b[_cons]-2*_se[_cons])
matrix CI[2, `i'] = 100*(_b[_cons]+2*_se[_cons])
}

matrix list median
matrix list CI

coefplot matrix(median), ci(CI) vertical  ///
ytitle("Perc. change in # of subs per player",size(large)) ///
xtitle("Percentage change in # of competitors",size(large)) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
ylabel(,nogrid labsize(large)) xlabel(,nogrid labsize(large))   scheme(sj) 
graph export Output/Figure7D.pdf, replace


*******Figure A3

clear
insheet using sigmaT.csv
rename v1 competitionid
joinby competitionid using competitioninfo, unmatched(master)
drop _merge

gen B = (v5-v4)*rewardquantity/40

gen expcost = v2/(1+v2)
sum expcost
hist expcost , freq bin(20) xline(`=r(mean)' ) ytitle(,size(large)) xtitle("Expected cost of team formation",size(large)) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
ylabel(,nogrid labsize(large)) xlabel(,nogrid labsize(large))   scheme(sj) ///
text(10 `=r(mean)' `"mean = `=string(`=r(mean)',"%6.2f")'"', ///
        color(red) placement(se))
graph export Output/FigureA3A.pdf, replace

gen expcost_dollars = rewardquantity*v2/(1+v2)
sum expcost_dollars
hist expcost_dollars , freq bin(20) xline(`=r(mean)' ) ytitle(,size(large)) xtitle("Expected cost of team formation (in USD)",size(large)) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
ylabel(,nogrid labsize(large)) xlabel(,nogrid labsize(large))   scheme(sj) ///
text(20 `=r(mean)' `"mean = `=string(`=r(mean)',"%6.2f")'"', ///
        color(red) placement(se))
graph export Output/FigureA3B.pdf, replace

gen expcost_cond_dollars = B*v2/(1+v2)
sum expcost_cond_dollars
hist expcost_cond_dollars , freq bin(20) xline(`=r(mean)' ) ytitle(,size(large)) xtitle("Expected cond. cost of team formation (in USD)",size(large)) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
ylabel(,nogrid labsize(large)) xlabel(,nogrid labsize(large))   scheme(sj) ///
text(50 `=r(mean)' `"mean = `=string(`=r(mean)',"%6.2f")'"', ///
        color(red) placement(se))
graph export Output/FigureA3C.pdf, replace


